package com.neusoft.movie.v_movies.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.neusoft.movie.v_movies.R;
import com.neusoft.movie.v_movies.common.Constants;
import com.neusoft.movie.v_movies.javabean.ResultBean;
import com.neusoft.movie.v_movies.javabean.UsersBean;
import com.neusoft.movie.v_movies.listener.RetrofitListener;
import com.neusoft.movie.v_movies.model.UsersModel;

public class ModifyMyInfoActivity extends BaseActivity implements RetrofitListener {

    private Context mContext;
    private String result;
    private int u_id;
    private EditText uname, upwd, uemail, uaddress, uphone;
    //private String u_name, u_pwd, u_email, u_address, u_phone;
    private Button button_modify_info;
    private ImageView left;

    @Override
    void initViews() {
        setLayout(R.layout.activity_modify_my_info);

        uname = (EditText) findViewById(R.id.u_name);
        upwd = (EditText) findViewById(R.id.u_pwd);
        uemail = (EditText) findViewById(R.id.u_email);
        uaddress = (EditText) findViewById(R.id.u_address);
        uphone = (EditText) findViewById(R.id.u_phone);

        button_modify_info = (Button) findViewById(R.id.button_modify_info);

        left = (ImageView) findViewById(R.id.left_button);

    }

    @Override
    void initData() {
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        u_id = sharedPreferences.getInt("u_id", 0);

        getUserInfo();
    }

    void getUserInfo() {
        UsersModel usersModel = new UsersModel();
        usersModel.getUserInfo(u_id, this);
    }

    void updateuser() {
        UsersModel usersModel = new UsersModel();
        usersModel.updateuser(
                u_id,
                uname.getText().toString(),
                uphone.getText().toString(),
                uemail.getText().toString(),
                uaddress.getText().toString(),
                upwd.getText().toString(), this);

    }

    @Override
    void initEvents() {
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        button_modify_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateuser();
            }
        });

    }

    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag) {
            case Constants.GETUSERINFO:
                UsersBean usersBean = (UsersBean) o;
                uname.setText(usersBean.getU_name());
                uaddress.setText(usersBean.getU_address());
                uemail.setText(usersBean.getU_email());
                uphone.setText(usersBean.getU_phone());
                upwd.setText(usersBean.getU_pwd());
                break;

            case Constants.UPDATEUSER:
                ResultBean resultBean=(ResultBean) o;
                if ("1".equals(resultBean.getSuccess())) {
                    Toast.makeText(ModifyMyInfoActivity.this, "修改成功", Toast.LENGTH_LONG).show();
                    finish();

                }else{
                    Toast.makeText(ModifyMyInfoActivity.this, "失败", Toast.LENGTH_LONG).show();
                    finish();
                }
                break;

                }
    }
        @Override
        public void onFailed () {

        }

}